package edu.hunau.aop.factory;

import edu.hunau.aop.advice.MethodIntercept;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @author hb
 * @date 2022-11-29 11:48:33
 */
public class ProxyFactoryBean  implements InvocationHandler {
    private Object target;// 目标对象
    public void setTarget(Object target) {	this.target = target;}
    private Object interceptorManager;
    public void setInterceptorManager(Object interceptorManager) {
        this.interceptorManager = interceptorManager;
    }
    // 执行目标方法之前，执行额外功能
    public Object invoke(Object proxy, Method method, Object[] args)
            throws Throwable {
        // 额外功能	TransactionManager tx = new TransactionManager();
//		tx.begin();
        Object obj=null;
        if(interceptorManager instanceof MethodIntercept){
            MethodIntercept inter=(MethodIntercept)interceptorManager;
            inter.before();
            obj = method.invoke(target, args);//执行指定的额外功能，返回结果
            inter.after();
        }	//tx.commit();
        return obj;	}
}

